package com.crs.mapper;

import com.crs.dto.UserLendDTO;
import com.crs.entity.Borrow_record;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BorrowMapper {
    /**
     * 用户借用接口
     * 在申请表中加入用户借用记录
     * @param
     */
    void insert(Borrow_record record);

    /**
     * 查找借阅状态是status==2并且还有不到24小时就会过期的记录
     */
    //TODO 莫名奇妙爆红，但是检查没问题修改掉颜色了
    List<Borrow_record> findOverTimeAndStatus();

    /**
     * 已经提醒过的用户不再提醒
     * @param br
     */
    @Update("update borrow_record set has_reminded = #{br.hasReminded} where id = #{br.id}")
    void updateOverTimeAndStatus(Borrow_record br);

    /**
     * 用户归还资源
     * @param
     */
    @Update("update borrow_record set status = 3 and actual_return = now() where id = #{resourceId}")
    void userReturn(Long resourceId);

    /**
     * 获取已经逾期的资源
     */
    @Select("select * from borrow_record where status = 2 and expected_return < now()")
    List<Borrow_record> findHasOverTime();

    /**
     * 针对已逾期的记录进行处理
     */
    @Update("update borrow_record set status = 4 where id = #{by.id}")
    void updateHasOverTime(Borrow_record by);

    /**
     * 获取已经逾期的资源
     * @return
     */
    @Select("select * from borrow_record where status = 4")
    List<Borrow_record> modifyHasOverTime();

    /**
     * 修改罚款金额
     * @param bo
     */
    @Update("update borrow_record set penalty_amount = #{bo.penaltyAmount} where id = #{bo.id}")
    void updatePenaltyAmount(Borrow_record bo);

    /**
     * 通过用户id和资源id查找对应的资源记录
     * @param resourceId
     * @param currentId
     * @return
     */
    @Select("select * from borrow_record where resource_id = #{resourceId} and user_id = #{currentId}")
    Borrow_record getBorrowRecords(Long resourceId, Long currentId);

    /**
     * 获取用户记录
     * @param recordsId
     * @return
     */
    @Select("select * from borrow_record where id = #{recordsId}")
    Borrow_record findById(Long recordsId);
}
